package com.shadow.demo.shiro.controller.sys;

import com.shadow.demo.common.util.SpringContextUtils;
import com.shadow.demo.data.bo.LoginUser;
import com.shadow.demo.shiro.dto.PermissionDTO;
import com.shadow.demo.shiro.service.PermissionService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SystemController {

    @Resource
    private PermissionService permissionService;

    /**
     * 首页.
     *
     * @return {@link String} 首页
     */
    @GetMapping("index")
    public String index(final ModelMap model) {
        List<PermissionDTO> menuList = permissionService.getPermissionListByUserAccount(getCurrentLoginUser().getAccount());
        model.addAttribute("menuList", menuList);
        return "index/index_zoneland";
    }

    /**
     * 获取当前登录人信息.
     *
     * @return {@link LoginUser} 当前登录人
     **/
    public static LoginUser getCurrentLoginUser() {
        return (LoginUser) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 获取菜单列表.
     *
     * @return {@link java.util.Set} 权限列表
     **/
    public static List<String> getMenuList() {
        final LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return new ArrayList<>(user.getPermissions());
    }

    /**
     * 获取菜单列表.
     *
     * @return {@link java.util.Set} 权限列表
     **/
    public static List<PermissionDTO> getMenuListNow() {
        PermissionService permissionBean = SpringContextUtils.getBean(PermissionService.class);
        return permissionBean.getPermissionListByUserAccount(getCurrentLoginUser().getAccount());
    }
}
